{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0342fb59-4060-4971-94bd-5aeadd98533a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Visible devices: [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]\n",
      "Tensor A:\n",
      "[[1. 2.]\n",
      " [3. 4.]]\n",
      "\n",
      "Tensor B:\n",
      "[[5. 6.]\n",
      " [7. 8.]]\n",
      "\n",
      "Addition (A + B):\n",
      "[[ 6.  8.]\n",
      " [10. 12.]]\n",
      "\n",
      "Subtraction (A - B):\n",
      "[[-4. -4.]\n",
      " [-4. -4.]]\n",
      "\n",
      "Multiplication (A * B):\n",
      "[[ 5. 12.]\n",
      " [21. 32.]]\n",
      "\n",
      "Division (A / B):\n",
      "[[0.2        0.33333334]\n",
      " [0.42857143 0.5       ]]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "# 强制 TensorFlow 仅使用 CPU\n",
    "tf.config.set_visible_devices([], 'GPU')  # 隐藏所有 GPU\n",
    "\n",
    "# 检查当前可见设备\n",
    "visible_devices = tf.config.get_visible_devices()\n",
    "print(\"Visible devices:\", visible_devices)\n",
    "\n",
    "# 创建张量并进行运算\n",
    "tensor_a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)\n",
    "tensor_b = tf.constant([[5, 6], [7, 8]], dtype=tf.float32)\n",
    "\n",
    "# 加法\n",
    "tensor_add = tensor_a + tensor_b  # 等价于 tf.add(tensor_a, tensor_b)\n",
    "\n",
    "# 减法\n",
    "tensor_sub = tensor_a - tensor_b  # 等价于 tf.subtract(tensor_a, tensor_b)\n",
    "\n",
    "# 乘法（逐元素相乘）\n",
    "tensor_mul = tensor_a * tensor_b  # 等价于 tf.multiply(tensor_a, tensor_b)\n",
    "\n",
    "# 除法（逐元素相除）\n",
    "tensor_div = tensor_a / tensor_b  # 等价于 tf.divide(tensor_a, tensor_b)\n",
    "\n",
    "# 打印结果\n",
    "print(\"Tensor A:\")\n",
    "print(tensor_a.numpy())  # 转换为 NumPy 数组以便打印\n",
    "\n",
    "print(\"\\nTensor B:\")\n",
    "print(tensor_b.numpy())\n",
    "\n",
    "print(\"\\nAddition (A + B):\")\n",
    "print(tensor_add.numpy())\n",
    "\n",
    "print(\"\\nSubtraction (A - B):\")\n",
    "print(tensor_sub.numpy())\n",
    "\n",
    "print(\"\\nMultiplication (A * B):\")\n",
    "print(tensor_mul.numpy())\n",
    "\n",
    "print(\"\\nDivision (A / B):\")\n",
    "print(tensor_div.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "385db2e0-3352-4443-a00a-018ea1c9b6f4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.16",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
